classdef SingleNodePacketDepositor < ClockHandler
    properties
        packet_template;
        ticks_to_deposit_packets;
        num_of_packets_to_deposit_in_each_tick = 0;
    end
    
    methods
        function obj = SingleNodePacketDepositor(source_node, dest_node, packet_size_in_bytes, num_of_packets_to_deposit,ticks_to_deposit_packets)
            
            if nargin>0
                packets_manager = PacketsManager.instance();
                obj.packet_template = packets_manager.create_template_packet(source_node, dest_node, packet_size_in_bytes);
                obj.ticks_to_deposit_packets = ticks_to_deposit_packets;
                obj.num_of_packets_to_deposit_in_each_tick = num_of_packets_to_deposit;
            end
            
            % register on clock tick
            ck = ClockSingleton.instance();
            ck.register_for_clock_ticks(obj);
        end
        
        function clock_tick_handler(obj)
            ck = ClockSingleton.instance();
            current_tick = ck.tick_count;
            
            should_deposit_packets = false;
            for i = 1:length(obj.ticks_to_deposit_packets)
                if obj.ticks_to_deposit_packets(i) == current_tick
                    should_deposit_packets = true;
                    break;
                end
            end
            
            if should_deposit_packets
                number_of_packets = obj.num_of_packets_to_deposit_in_each_tick;
                packets_manager = PacketsManager.instance();
                for i = 1:number_of_packets
                    packets_manager.create_new_packet(obj.packet_template);
                end
            end
        end
        
    end
end